/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useEffect, useRef, useState } from 'react';
import DragTable from '@/components/DragTable';
import type { Columns } from '../../_utils/type';
import { getColumns } from '../../_utils/index.config';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import { getRefundReviewList, refundReviewDel } from '@/services/tools/refundRequest';
import { usePaginationCache } from '@base/components/keep-alive/_utils';
import ProTable from '@ant-design/pro-table';
import { Popconfirm, Space, message } from 'antd';
import KbAddRefundExamine from '../add-examine';

interface KbRefundReviewProps {
  show: boolean;
  status: string;
}
const KbRefundReview = (props: KbRefundReviewProps) => {
  const { show, status } = props;
  const actionRef = useRef<ActionType>();
  const [record, setRecord] = useState<Columns | any>();
  const [visible, setVisible] = useState<boolean>(false);

  const handleDel = (id: string) => {
    refundReviewDel({ id }).then((res) => {
      const { code, msg } = res;
      if (code === 0) {
        actionRef.current?.reload();
      } else {
        message.error(msg);
      }
    });
  };

  const handleModify = (record_: Columns) => {
    setRecord(record_);
    setVisible(true);
  };

  const onCancel = () => {
    setRecord({});
    setVisible(false);
    actionRef.current?.reload();
  };

  const columns: ProColumns[] = [
    ...getColumns({ status }),
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      align: 'center',
      width: 100,
      render: (_: any, record_: Columns) => {
        const { id, status: status_ } = record_;
        const d = Number(status_);
        return d === 0 ? (
          <Space>
            <Popconfirm title='你确定删除？' onConfirm={handleDel.bind(null, id)}>
              <a>删除</a>
            </Popconfirm>
            <a onClick={handleModify.bind(null, record_)}>修改</a>
          </Space>
        ) : d === 2 ? (
          <a onClick={handleModify.bind(null, record_)}>重新提交</a>
        ) : null;
      },
    },
  ];

  const pagination = usePaginationCache(`table${status}`, {
    pageSize: 20,
    showQuickJumper: false,
    showSizeChanger: false,
  });

  useEffect(() => {
    if (show) {
      actionRef.current?.reload();
    }
  }, [show]);

  return (
    <>
      <DragTable>
        <ProTable<ProColumns<Columns[]>>
          rowKey='id'
          toolBarRender={false}
          pagination={pagination}
          request={getRefundReviewList}
          params={{ status }}
          actionRef={actionRef}
          columns={columns}
          scroll={{ x: 1200 }}
        />
      </DragTable>
      <KbAddRefundExamine
        record={record}
        type='modal'
        visible={visible}
        title='修改'
        modalProps={{
          onCancel,
        }}
        callback={onCancel}
      />
    </>
  );
};

export default KbRefundReview;
